package com.detrum.detrumdx.fragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.detrum.detrumdx.R;
import com.detrum.detrumdx.activity.SelfInfoActivity;
import com.detrum.detrumdx.activity.SelfSetting;
import com.detrum.shopping.web.ForumWebActivity;
import com.detrum.shopping.web.MainWebActivity;
import com.detrum.shopping.web.ShopWebActivity;
import com.detrum.utils.MyMethod;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link ShopFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ShopFragment extends Fragment implements View.OnClickListener{

    private OnFragmentInteractionListener mListener;

    private Context context;
    private RelativeLayout mRlAdvice,mRlForum,mRlMall,mRlSettings,mRlInfo;
    private LinearLayout detrum_shop,detrum_main,detrum_forum;

    public static ShopFragment newInstance() {
        ShopFragment fragment = new ShopFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shop, container, false);
        initView(view);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    private void initView(View parentView) {
        mRlAdvice = parentView.findViewById(R.id.detrum_fans_advice);
        mRlAdvice.setOnClickListener(this);
        mRlForum = parentView.findViewById(R.id.detrum_fans_forum);
        mRlForum.setOnClickListener(this);
        mRlMall = parentView.findViewById(R.id.detrum_fans_mall);
        mRlMall.setOnClickListener(this);
        mRlSettings = parentView.findViewById(R.id.detrume_me_settings);
        mRlSettings.setOnClickListener(this);
        mRlInfo = parentView.findViewById(R.id.detrum_fans_info);
        mRlInfo.setOnClickListener(this);
        detrum_shop = parentView.findViewById(R.id.detrum_shop);
        detrum_shop.setOnClickListener(this);
        detrum_forum = parentView.findViewById(R.id.detrum_forum);
        detrum_forum.setOnClickListener(this);
        detrum_main = parentView.findViewById(R.id.detrum_main);
        detrum_main.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.detrum_fans_advice:// 版本检测
                mListener.downloadVersion();
                break;
            case R.id.detrum_fans_forum:// 论坛
                MyMethod.toActivity(context, ForumWebActivity.class);
                break;
            case R.id.detrum_fans_mall:// 商城
                MyMethod.toActivity(context, ShopWebActivity.class);
                break;
            case R.id.detrume_me_settings:// 设置
                MyMethod.toActivity(context, SelfSetting.class);
                break;
            case R.id.detrum_fans_info://个人信息
                MyMethod.toActivity(context, SelfInfoActivity.class);
                break;
            case R.id.detrum_main:
                MyMethod.toActivity(context, MainWebActivity.class);
                break;
            case R.id.detrum_forum:
                MyMethod.toActivity(context, ForumWebActivity.class);
                break;
            case R.id.detrum_shop:
                MyMethod.toActivity(context, ShopWebActivity.class);
                break;
            default:
                break;
        }
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(Uri uri);
        void downloadVersion();
    }
}
